﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

using CE.User;
using CE.Global;
using CE.Configuration;
using CE.Provider;

public partial class Provider_CarInsurance_ProviderCarMakeOnly : System.Web.UI.Page
{
    int _carmakeid = 0;
    Int64 _carmakemodelid = 0;
    int _providercarmakeid = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        //usrLink.PageName = "Provider Car Make Model";

        if (Session[CE.Global.General.UserId] == null)
        {
            Response.Redirect("~/Provider/ProviderSecureLogin.aspx");
        }
        if (!Page.IsPostBack)
        {
            GetCXCarMake();            

            if (Request.Params["CarMakeId"] != null)
            {
                if (int.TryParse(Request.Params["CarMakeId"].ToString(), out _carmakeid))
                {                    
                    //ddlProviderCarMake_SelectedIndexChanged(null, null);
                    ShowCarMake(_carmakeid, Convert.ToInt64(Session[General.ProviderCompanyId].ToString()));
                    GetCarMakeDetails(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()), _carmakeid);
                    ddlCXCarMake.SelectedValue = _carmakeid.ToString();
                }
            }
           
        }

    }
    

    protected void ddlCXCarMake_SelectedIndexChanged(object sender, EventArgs e)
    {
        ClearFields();
        ShowCarMake(Convert.ToInt32(ddlCXCarMake.Text), Convert.ToInt64(Session[General.ProviderCompanyId].ToString()));

        GetCarMakeDetails(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()), Convert.ToInt32(ddlCXCarMake.Text));
    }

    private void GetCXCarMake()
    {
        DataTable dtCarMake = General.GetComboItem("CarMake", " [Description]", true);
        ddlCXCarMake.DataSource = dtCarMake;
        ddlCXCarMake.DataTextField = dtCarMake.Columns[0].ColumnName.ToString();
        ddlCXCarMake.DataValueField = dtCarMake.Columns[1].ColumnName.ToString();
        ddlCXCarMake.DataBind();
        ddlCXCarMake.Items.Insert(0, new ListItem("Select", "-1"));
        dtCarMake = null;


        //CE.CarInsurance.CarMakeList _cxcarmakelist = new CE.CarInsurance.CarMakeList();
        //CE.CarInsurance.CarMakeDB _cxcarmakedb = new CE.CarInsurance.CarMakeDB();

        //_cxcarmakelist = _cxcarmakedb.GetCarMakeList();
        //if (_cxcarmakelist != null)
        //{
        //    for (int i = 0; i < _cxcarmakelist.Count; i++)
        //    {
        //        ddlCXCarMake.Items.Add(new ListItem(_cxcarmakelist[i].Description, _cxcarmakelist[i].CarMakeId.ToString()));
        //    }
        //}
        //ddlCXCarMake.Items.Insert(0, new ListItem("Select", "-1"));
    }

    protected void ShowCarMake(int CarMakeId, Int64 ProviderCompanyId)
    {
        CarMake _carmake = new CarMake();
        CarMakeDB _carmakedb = new CarMakeDB();

        _carmake = _carmakedb.GetProviderCarMake(CarMakeId, ProviderCompanyId);

        //_providercarmakeid = 0;

        if (_carmake != null)
        {
            txtprovidercarmakeid.Text = _carmake.ProviderCarMakeId.ToString();
            //_providercarmakeid = _carmake.ProviderCarMakeId;
            txtMyCarMake.Text = _carmake.Description;            
            chkStatus.Checked = _carmake.Status;
        }
        else
        {
            txtprovidercarmakeid.Text = "0";
            txtprovidercarmakemodelid.Text = "0";
            ClearFields();
        }
    }
    
    private void GetCXCarModel(Int32 CarMakeId)
    {
        //CarModelDB _providercarModeldb = new CarModelDB();
        //DataTable dtCarModel = _providercarModeldb.GetCarModelDT(Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId].ToString()));       
        //ddlCXCarModel.Items.Clear();
        CE.CarInsurance.CarModelList _carmodellist = new CE.CarInsurance.CarModelList();
        CE.CarInsurance.CarModelDB _carmodeldb = new CE.CarInsurance.CarModelDB();
        _carmodellist = _carmodeldb.GetCarModelList(CarMakeId);

        if (_carmodellist != null)
        {
            for (int i = 0; i < _carmodellist.Count; i++)
            {
                //ddlCXCarModel.Items.Add(new ListItem(_carmodellist[i].Description, _carmodellist[i].CarModelId.ToString()));
            }
        }

        //ddlCXCarModel.Items.Insert(0, new ListItem("Select", "-1"));

    }

    private void GetCarMakeDetails(Int64 ProviderCompanyId, Int32 CarMakeId)
    {
        CarMakeModel _providercarmakemodel = new CarMakeModel();
        CarMakeModelDB _providercarmakemodeldb = new CarMakeModelDB();

        _providercarmakemodel = _providercarmakemodeldb.GetProviderCarMakeItemOnly(ProviderCompanyId, CarMakeId);

        

        if (_providercarmakemodel != null)
        {
            txtprovidercarmakemodelid.Text = _providercarmakemodel.ProviderCarMakeModelId.ToString();
            //_carmakemodelid = _providercarmakemodel.ProviderCarMakeModelId;
            //txtRate.Text = _providercarmakemodel.Rate == 0 ? "" : _providercarmakemodel.Rate.ToString("#.00");
            txtRate.Text = _providercarmakemodel.Rate.ToString();
            rdoRefer.SelectedValue = _providercarmakemodel.IsRefer.ToString();
            

        }
    
    }

    protected void btnSave_OnClick(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            string _authcode = CE.Security.MySecurity.Encrypt(txtAuthCode.Text, true);
            if (CheckAuthCode(_authcode, 100))
            {

                //
                CarMake _carmake = new CarMake();
                CarMakeDB _carmakedb = new CarMakeDB();

                _carmake.ProviderCarMakeId = Convert.ToInt32(txtprovidercarmakeid.Text); // CheckCarMake(Convert.ToInt32(ddlCXCarMake.Text), Convert.ToInt64(Session[General.ProviderCompanyId].ToString()));
                int _trantype = 1;
                if (_carmake.ProviderCarMakeId > 0)
                {
                    _trantype = 2;
                }
                _carmake.ProviderCompanyId = Convert.ToInt64(Session[General.ProviderCompanyId].ToString());
                _carmake.RefCEUserId = Convert.ToInt64(Session[General.UserId].ToString());

                _carmake.CarMakeId = Convert.ToInt32(ddlCXCarMake.Text);
                _carmake.Description = txtMyCarMake.Text.Trim();
                _carmake.Status = chkStatus.Checked;

                _carmake.ProviderCarMakeId = _carmakedb.Insert(_carmake);
                InsertProviderAuthTransaction(_carmake.ProviderCarMakeId, "CICarMake", _trantype, 100, _authcode, _carmake.ProviderCompanyId, _carmake.RefCEUserId);

                //

                if (_carmake.ProviderCarMakeId > 0)
                {
                    CarMakeModel _providercarmakemodel = new CarMakeModel();
                    CarMakeModelDB _providercarmakemodeldb = new CarMakeModelDB();

                    _providercarmakemodel.ProviderCarMakeModelId = Convert.ToInt64(txtprovidercarmakemodelid.Text);
                    int _trantypes = 1;
                    if (_providercarmakemodel.ProviderCarMakeModelId > 0)
                    {
                        _trantypes = 2;
                    }
                    _providercarmakemodel.CarMakeId = Convert.ToInt32(ddlCXCarMake.SelectedValue);
                    _providercarmakemodel.CarModelId = -1;
                    _providercarmakemodel.ProviderCompanyId = Convert.ToInt64(Session[General.ProviderCompanyId].ToString());
                    _providercarmakemodel.Description = ddlCXCarMake.SelectedItem.Text.ToString();
                    _providercarmakemodel.CarCode = "na";
                    _providercarmakemodel.Body = "na";
                    _providercarmakemodel.NumberOfDoors = 4;
                    _providercarmakemodel.CC = 1000;
                    _providercarmakemodel.TurboEngine = false;
                    _providercarmakemodel.ParallelImport = false;
                    _providercarmakemodel.Rate = float.Parse(txtRate.Text);
                    _providercarmakemodel.Agent = 0;
                    _providercarmakemodel.UW = 0;
                    _providercarmakemodel.RefCEUserId = Convert.ToInt64(Session[CE.Global.General.UserId].ToString());
                    _providercarmakemodel.IsRefer = bool.Parse(rdoRefer.SelectedValue);
                    _providercarmakemodel.Status = chkStatus.Checked;

                    _providercarmakemodel.ProviderCarMakeModelId = _providercarmakemodeldb.Insert(_providercarmakemodel);
                    if (_providercarmakemodel.ProviderCarMakeModelId > 0)
                    {
                        InsertProviderAuthTransaction(_providercarmakemodel.ProviderCarMakeModelId, "CICarMakeModel", _trantypes, 100, _authcode, _providercarmakemodel.ProviderCompanyId, _providercarmakemodel.RefCEUserId);
                        lblMessage.Text = "Updated successfully!";
                        //Response.Redirect("ViewProviderCarMakeOnly.aspx");
                    }
                    else
                    {
                        lblMessage.Text = "Updated failed.";
                    }
                }
            }
            else
            {
                lblMessage.Text = "Wrong auth code!";
            }
        }
    }

    protected void ibtBack_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("ViewProviderCarMakeOnly.aspx");
    }

    private void ClearFields()
    {
        txtMyCarMake.Text = "";
        txtRate.Text = "0";
        rdoRefer.Text = "True";

    }

    protected bool CheckAuthCode(string strAuthCode, int intAuthCodeType)
    {
        bool _retValue = false;
        ProviderAuthCode _authcode = new ProviderAuthCode();
        ProviderAuthCodeDB _authcodedb = new ProviderAuthCodeDB();
        _authcode = _authcodedb.GetProviderAuthCode(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()), intAuthCodeType, strAuthCode);
        if (_authcode != null)
        {
            _retValue = true;
        }

        return _retValue;
    }

    protected Int64 InsertProviderAuthTransaction(Int64 TransactionId, string TransactionCode, int TransactionType,
        int AuthCodeType, string AuthCode, Int64 ProviderCompanyId, Int64 RefCEUserId)
    {
        ProviderAuthTransaction _authtrans = new ProviderAuthTransaction();
        ProviderAuthTransactionDB _authtransdb = new ProviderAuthTransactionDB();

        _authtrans.TransactionId = TransactionId;
        _authtrans.TransactionCode = TransactionCode;
        _authtrans.TransactionType = TransactionType;
        _authtrans.AuthCode = AuthCode;
        _authtrans.AuthCodeType = AuthCodeType;
        _authtrans.ProviderCompanyId = ProviderCompanyId;
        _authtrans.RefCEUserID = RefCEUserId;

        _authtrans.AuthTransId = _authtransdb.Insert(_authtrans);
        return _authtrans.AuthTransId;
    }
 
}
